﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace RpaDesigner.Frame
{
    // [TemplatePart(Name = "PART_DragThumb", Type = typeof(DragThumb))]
    // [TemplatePart(Name = "PART_ResizeDecorator", Type = typeof(Control))]
    // [TemplatePart(Name = "PART_ConnectorDecorator", Type = typeof(Control))]
    // [TemplatePart(Name = "PART_ContentPresenter", Type = typeof(ContentPresenter))]
    public class DesignerItem : ContentControl, ISelectable, IGroupable
    {
        # region Id

        public Guid Id { get; }

        # endregion

        # region ParentId

        public static readonly DependencyProperty ParentIdProperty =
            DependencyProperty.Register(nameof(ParentId), typeof(Guid), typeof(DesignerItem));

        public Guid ParentId
        {
            get => (Guid) GetValue(ParentIdProperty);
            set => SetValue(ParentIdProperty, value);
        }

        # endregion

        #region IsGroup

        public bool IsGroup
        {
            get { return (bool) GetValue(IsGroupProperty); }
            set { SetValue(IsGroupProperty, value); }
        }

        public static readonly DependencyProperty IsGroupProperty =
            DependencyProperty.Register("IsGroup", typeof(bool), typeof(DesignerItem));

        #endregion

        #region IsSelected Property

        public bool IsSelected
        {
            get => (bool) GetValue(IsSelectedProperty);
            set => SetValue(IsSelectedProperty, value);
        }

        public static readonly DependencyProperty IsSelectedProperty =
            DependencyProperty.Register("IsSelected",
                typeof(bool),
                typeof(DesignerItem),
                new FrameworkPropertyMetadata(false));

        #endregion

        #region 描述

        public static readonly DependencyProperty ItemDescProperty =
            DependencyProperty.Register("ItemDesc", typeof(string), typeof(DesignerItem));

        public string ItemDesc
        {
            get => (string) GetValue(ItemDescProperty);
            set => SetValue(ItemDescProperty, value);
        }

        #endregion

        #region ConnectorDecoratorTemplate Property

        // can be used to replace the default template for the ConnectorDecorator
        public static readonly DependencyProperty ConnectorDecoratorTemplateProperty =
            DependencyProperty.RegisterAttached("ConnectorDecoratorTemplate", typeof(ControlTemplate),
                typeof(DesignerItem));

        public static ControlTemplate GetConnectorDecoratorTemplate(UIElement element)
        {
            return (ControlTemplate) element.GetValue(ConnectorDecoratorTemplateProperty);
        }

        public static void SetConnectorDecoratorTemplate(UIElement element, ControlTemplate value)
        {
            element.SetValue(ConnectorDecoratorTemplateProperty, value);
        }

        #endregion

        #region IsDragConnectionOver

        // while drag connection procedure is ongoing and the mouse moves over 
        // this item this value is true; if true the ConnectorDecorator is triggered
        // to be visible, see template
        public bool IsDragConnectionOver
        {
            get => (bool) GetValue(IsDragConnectionOverProperty);
            set => SetValue(IsDragConnectionOverProperty, value);
        }

        public static readonly DependencyProperty IsDragConnectionOverProperty =
            DependencyProperty.Register("IsDragConnectionOver",
                typeof(bool),
                typeof(DesignerItem),
                new FrameworkPropertyMetadata(false));

        #endregion
        
        public DesignerItem(string itemDesc)
        {
            Id = Guid.NewGuid();
            ItemDesc = itemDesc;

            this.Loaded += new RoutedEventHandler(DesignerItem_Loaded);
        }

        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseDown(e);
            DesignerCanvas designer = VisualTreeHelper.GetParent(this) as DesignerCanvas;

            // update selection
            if (designer != null)
            {
                if ((Keyboard.Modifiers & (ModifierKeys.Shift | ModifierKeys.Control)) != ModifierKeys.None)
                    if (this.IsSelected)
                    {
                        designer.SelectionService.RemoveFromSelection(this);
                    }
                    else
                    {
                        designer.SelectionService.AddToSelection(this);
                    }
                else if (!this.IsSelected)
                {
                    designer.SelectionService.SelectItem(this);
                }

                Focus();
            }

            e.Handled = false;
        }

        private void DesignerItem_Loaded(object sender, RoutedEventArgs e)
        {
            // if (base.Template != null)
            // {
            //     ContentPresenter contentPresenter =
            //         this.Template.FindName("PART_ContentPresenter", this) as ContentPresenter;
            //     if (contentPresenter != null)
            //     {
            //         UIElement contentVisual = VisualTreeHelper.GetChild(contentPresenter, 0) as UIElement;
            //         if (contentVisual != null)
            //         {
            //             DragThumb thumb = this.Template.FindName("PART_DragThumb", this) as DragThumb;
            //             if (thumb != null)
            //             {
            //                 ControlTemplate template =
            //                     DesignerItem.GetDragThumbTemplate(contentVisual) as ControlTemplate;
            //                 if (template != null)
            //                     thumb.Template = template;
            //             }
            //         }
            //     }
            // }
        }
    }
}